<template>
    <div class="user-box">
        <div class="header"></div>
        <div class="body">
            <router-view/>
        </div>
        <div class="footer"></div>
        <div style="display: none">
            <audio ref="audio" src="http://www.yumingzzz.xyz/cash.mp3" controls="controls">
                Your browser does not support the audio tag.
            </audio>
        </div>
    </div>
</template>

<script>
    import apiUrl from "../../api/apiUrl";

    export default {
        name: "index",
        data () {
            return {
                mp3Src: 'http://www.yumingzzz.xyz/cash.mp3',
                timer: null
            }
        },
        beforeRouteEnter(to,from,next) {
            if (sessionStorage.getItem('user_type') === 'room') {
                next(vm => {
                    // vm.play('claim')
                    vm.loadMore()
                })
            } else {
                next({name: 'login'})
            }
        },
        methods: {
            loadMore() {
                this.timer = window.setInterval(() => {
                    this.setData()
                }, 5000)
            },
            setData() {
                this.$http.get(apiUrl.room.audioRequest).then(res => {
                    if (res.data.code === 0) {
                        if (res.data.result.cash) {
                            this.play('cash')
                        }
                        if (res.data.result.claim) {
                            this.play('claim')
                        }
                    } else {

                    }
                }).catch(err => {
                    console.log(err)
                })
            },
            play (type = 'cash') {
                if (type === 'cash') {
                    this.$refs.audio.src = 'http://www.yumingzzz.xyz/cash.mp3'
                } else {
                    this.$refs.audio.src = 'http://www.yumingzzz.xyz/claim.mp3'
                }
                this.$refs.audio.play()
            }
        },
        //离开当前页面后执行
        destroyed: function () {
            window.clearInterval(this.timer)
        },
    }
</script>

<style scoped>

</style>